// 二分查找法
function twoSum(nums: number[], target: number): number[] {
    const arr = nums.map((v, i) => ({ val: v, ind: i }));

    arr.sort((a, b) => a.val - b.val);

    for (let i = 0; i < arr.length - 2; i++) {

        const sec = target - arr[i].val;
        let l = i + 1;
        let r = arr.length - 1;
        while (l < r) {
            const m = Math.floor((r + l) / 2);
            const { val, ind: tInd } = arr[m];
            if (val === sec) {
                return [arr[i].ind, tInd]
            } else if (val < sec) {
                l = m + 1;
            } else {
                r = m - 1;
            }
        }
        if (arr[l].val === sec) {
            return [arr[i].ind, arr[l].ind]
        }
    }
    return []
};